
//------------------------------------------------------------//
//  TiGia.java
//  Author:GTVT
//  Lam Cac Nhiem Vu Cap Nhat Ti Gia Tu Trang
//  http://www.vietcombank.com.vn/ExchangeRates
//  Cap Nhat Du Lieu Ti Gia Lay Duoc Vao Co So Du Lieu
//------------------------------------------------------------//
import java.io.*;
import java.net.*;
import java.sql.SQLException;

public class TiGia {

    private static String url = "http://www.vietcombank.com.vn/ExchangeRates";

    public TiGia() {
    }

    public static void capnhat() throws Exception {

        //-----------------------------------------------------//
        //  Step 1: Khoi Tao
        //-----------------------------------------------------//
        URL u;
        InputStream is = null;
        DataInputStream dis;
        BufferedReader br;
        String s;

        u = new URL(url);
        // Step 3: Mo Kenh(Stream) De Doc Du Lieu  //
        is = u.openStream();         // throws an IOException

        // Step 4:                                                     //
        // Doi InputStream Sang Bo Dem DataInputStream.      //
        // Bo Dem Lam Cho Viec Doc Nhanh Hon; va          //
        // Phuong Thuc readLine() Cua DataInputStream lam cho viec doc  //
        // De Dang Hon                                                    //
        dis = new DataInputStream(new BufferedInputStream(is));
        br = new BufferedReader(new InputStreamReader(dis, "utf-8"));

        //Tao Ket Noi Voi Co So Du Lieu
        //Tao Table Ngoai Te De Chua Du Lieu
        DbSqlite db = new DbSqlite("mydatabase.db");
        db.update("create table if not exists NGOAITE(MANT,TENNT,MUATIENMAT,MUACHUYENKHOAN,BAN,THOIGIAN,PRIMARY KEY(MANT,THOIGIAN));");

        //Lay Ngay Thang Cap Nhat
        String thoigian = layNgay(br);

        is.close();
        dis.close();
        br.close();
        is = u.openStream();
        dis = new DataInputStream(new BufferedInputStream(is));
        br = new BufferedReader(new InputStreamReader(dis, "utf-8"));


        //Nhay Den Dong Co Chua Du Lieu Can Doc
        while ((s = br.readLine()) != null) {
            if (s.trim().startsWith("<table")) {
                break;
            }
        }

        //Khoi Tao Cac Bien Dau Hieu, Doc Va Xu Ly, Luu Vao Co So Du Lieu
        String dau = "<td class=\"code\">";
        String cuoi = "</td>";
        String tmp = "";
        while ((s = br.readLine()) != null) {


            if (s.indexOf(dau) > 0) {
                tmp = s.substring(s.indexOf(dau) + dau.length(), s.indexOf(cuoi)) + "#";
                s = s.substring(s.indexOf(cuoi) + cuoi.length());// lay chuoi con trong chuoi ban dau
                tmp += s.substring(s.indexOf(dau) + dau.length(), s.indexOf(cuoi)) + "#";
                s = s.substring(s.indexOf(cuoi) + cuoi.length());// lay chuoi con trong chuoi ban dau
                s = br.readLine();
                s = s.substring(s.lastIndexOf(" ") + 1);
                tmp += s + "#";
                s = br.readLine();
                dau = "</td><td>";
                cuoi = "</td><td>";
                tmp += s.substring(s.indexOf(dau) + dau.length(), s.lastIndexOf(cuoi)) + "#";
                s = br.readLine();
                tmp += s.trim() + "#";
                tmp += thoigian;

                try {
                    db.insert("NGOAITE", tmp, '#');
                } catch (SQLException ex) {
                    System.out.println(ex);
                }

                dau = "<td class=\"code\">";
                cuoi = "</td>";
            }

            if (s.trim().startsWith("</table")) {
                break;
            }

        }


        try {
            is.close();
        } catch (IOException ioe) {
            // just going to ignore this one
        }
        db.close();

    }

    private static String layNgay(BufferedReader br) throws Exception {
        String s = "";
        String dau = "<input name=\"ctl00$Content$DateText\" type=\"text\" value=\"";
        String cuoi = "\" maxlength=\"10\" id=\"ctl00_Content_DateText\" />";
        while ((s = br.readLine()) != null) {
            if (s.lastIndexOf(dau) > 0) {
                s = s.substring(s.lastIndexOf(dau) + dau.length(), s.lastIndexOf(cuoi));
                break;
            }
        }
        // br.reset();
        return s;
    }
} // end of class definitio

